var wc = require("../components/winLocation.js")
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;

var token, loginName, openid, loginType, balance, acc_id, bank_id, valid_balance;
var $takeSubmit, $takeMoneyInput;
var errorObject;
$(document).on("pageinit", "#takeMoney_page", controllerInit);

function controllerInit() {
    console.log("dfd");
    initValue();
    $takeSubmit.on("touchstart", getMoney);
    $takeMoneyInput.on("input", takeMoneChange);
    $("#take_all_btn").on("touchstart", function() {
        if (parseFloat(balance) < 1000000) {
            $takeMoneyInput.val(balance);
        } else {
            $takeMoneyInput.val(1000000.00);
        }
        if (parseFloat(balance) == 0.00) {
            $takeSubmit.attr("class", "weui-btn weui-btn_disabled weui-btn_default ui-link");
        } else {
            $takeSubmit.attr("class", "weui-btn weui-btn_primary ui-link");
        }
    });

    $("#fanhui").on("touchstart", function() {
        location.href = "./merchantHome.html";
    });
}

// 调取现
function getMoney() {
    var takemValue = $("#take_money").val();
    if ($takeSubmit.hasClass("weui-btn_disabled")) {
        return;
    } else if (parseFloat(takemValue) < 100 && parseFloat(balance) < 100 && parseFloat(takemValue) != parseFloat(balance)) {
        errorObject.showErroDialog("账户余额小于100,取现必须全部取出");
    } else if (parseFloat(takemValue) < 100 && parseFloat(balance) > 100) {
        errorObject.showErroDialog("账户余额大于100,取现必须大于100");
    } else {
        $takeSubmit.attr("class", "weui-btn weui-btn_disabled weui-btn_default ui-link");
        var gmParameters = {
            "token": token,
            "loginName": loginName,
            "openid": openid,
            "loginType": loginType,
            "bizLicNo": loginName,
            "acc_id": acc_id,
            "transout_amt": takemValue,
            "valid_balance": valid_balance
        };

        $.post(GetCashServlet_URL, gmParameters, gmoneyBack);
    }


}

// 取现回调函数
function gmoneyBack(gmdata) {
    $takeSubmit.attr("class", "weui-btn weui-btn_primary ui-link");
    if (gmdata.login_response) {
        haslogin(gmdata);
        return;
    }
    console.dir(gmdata);
    if (gmdata.card_get_money_response && gmdata.card_get_money_response.resp_code == "0000") {
        errorObject.showErroDialog("转出成功,3秒后返回商户首页");
        setTimeout(function() {
            history.go(-1);
        }, 3000);

    } else if (gmdata.error_response && gmdata.error_response.sub_msg) {
        errorObject.showErroDialog(gmdata.error_response.sub_msg);
    } else if (gmdata.error_response && gmdata.error_response.msg) {
        errorObject.showErroDialog(gmdata.error_response.msg);
    }
}

// 取现输入变化
function takeMoneChange() {
    var tmValue = $takeMoneyInput.val();
    var rexp = /\d{1,}.\d{2}/;
    if (rexp.test(tmValue)) { // 限制只能输入2位小数
        var needVal = tmValue.match(rexp)[0];
        $takeMoneyInput.val(needVal);
    }
    if (tmValue == "" || parseFloat(tmValue) == 0.00) {
        $takeSubmit.attr("class", "weui-btn weui-btn_disabled weui-btn_default ui-link");
    } else if (parseFloat(tmValue) > 1000000) {
        errorObject.showErroDialog("取现金额最大为100万");
        $takeMoneyInput.val("1000000.00");
    } else if (parseFloat(tmValue) > parseFloat(balance)) {
        errorObject.showErroDialog("取现金额不能大于账户余额");
        $takeMoneyInput.val(balance);
    } else if (parseFloat(tmValue) != 0.00) {
        $takeSubmit.attr("class", "weui-btn weui-btn_primary ui-link");
    }

}

// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
        errorObject.showErroDialog("该账户已在别的地方登录,请重新登入");
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    }
}
// 初始化变量
function initValue() {
    getSessionStorage();
    errorObject = new errorDialog();
    $("#balance").text(balance);
    $("#card_number").val(bank_id);
    $takeSubmit = $("#take_submit");
    $takeMoneyInput = $("#take_money");
    logoutObject = new Logout();
}

// 取本地存储essionStorage
function getSessionStorage() {
    token = sessionStorage.getItem("tltd_token");
    loginName = sessionStorage.getItem("tltd_loginName");
    openid = sessionStorage.getItem("tltd_openid");
    loginType = sessionStorage.getItem("tltd_loginType");
    balance = sessionStorage.getItem("balance"); // 取现账户余额
    acc_id = sessionStorage.getItem("acc_id"); // 取现 参数acc_id
    bank_id = sessionStorage.getItem("bank_id"); // 取现银行卡号
    valid_balance = sessionStorage.getItem("valid_balance"); // 取现 参数valid_balance
    console.log(token, loginName, openid, loginType, balance, acc_id, bank_id, valid_balance);
}